<?php


/*
 * Created on 03.01.2006
 *
 */

class phpinchains_tagsupport_URL
{

    var $value;
    var $params;
    var $action = '';
    var $module = '';
    var $anquor;
    var $actionParam;
    var $actionParamId;
    var $moduleParam;
    var $moduleParamId;
    var $parameterParam;
    var $medium;
    var $env;

    function phpinchains_tagsupport_URL(& $env, $value = '')
    {
        $this->value = $value;
        $this->env = & $env;
        $run = & phpinchains_RuntimeConfig :: getInstance();
        $this->actionParam =    $run->get("actionParam");
        $this->actionParamId =  $run->get("actionParamId");
        $this->moduleParam =    $run->get("moduleParam");
        $this->moduleParamId =  $run->get("moduleParamId");
        $this->parameterParam = $run->get("parameterParam");
    }

    function addParam($name, $value)
    {
        if ($value)
            $this->params[] = array (
                $name,
                $value
            );
    }

    function addParams($params)
    {
        if (is_array($params))
        	foreach ($params as $n => $v)
				$this->addParam($n, $v);
    }

    function setAction($action)
    {
        $this->action = $action;
    }

    function setMedium($medium)
    {
        $this->medium = $medium;
    }

    function setModule($module)
    {
        $this->module = $module;
    }

    function setAnquor($a)
    {
        $this->anquor = $a;
    }

    function toString()
    {
        // automatic doc_id transportation
        if (!empty($GLOBALS['doc_id'])) {
            $this->addParam('doc_id', $GLOBALS['doc_id']);
        }
        $paramstr = "";
        $params = array ();
        for ($i = 0; $i < count($this->params); $i++)
        {
            $this->params[$i][0] = urlencode($this->params[$i][0]); 
            if (!$this->params[$i][0])
                continue;
            if ($this->parameterParam == 'pathInfo')
            {
                if ($i != 0)
                    $paramstr .= "/";
                $paramstr .= join("/", $this->params[$i]);
            }
            else
            {
                if ($i != 0)
                    $paramstr .= "&";
                $paramstr .= join("=", $this->params[$i]);
            }
        }
        if ($paramstr)
            $params[] = $paramstr;
        if ($this->value)
            $url = $this->value;
        else
            $url = $this->env->getServer("SCRIPT_NAME");
        if (!empty($GLOBALS['innerScriptName'])) // set by DispatchLegacyAdminFilter from XICS
            $url .= '/admin/'.$GLOBALS['innerScriptName'];
        if (defined('MODEL2_SCRIPT_NAME'))
            $url = MODEL2_SCRIPT_NAME;
            
        if ($this->actionParam == 'pathInfo')
        {
            if ($this->module)
            {
                $url .= '/'.$this->module;
            }
            elseif ($this->moduleParam) 
            {
                $url .= '/'.$this->moduleParam;
            }
            $url .= '/'.$this->action;
        }
        else
        {
            $modPar = ($this->moduleParamId) ? $this->moduleParamId : 'module'; 
            if ($this->module)
            {
                $params[] = $modPar.'='.$this->module;
            }
            elseif ($this->moduleParam) 
            {
                $params[] = $modPar.'='.$this->moduleParam;
            }
            if ($this->action)
            {
                $actPar = ($this->actionParamId) ? $this->actionParamId : 'action';
                $params[] = $actPar.'='.$this->action;
            }
        }
        if (count($params))
        {
            if ($this->parameterParam == 'pathInfo')
                $url .= '/'.join("/", $params);
            else
                $url .= '?'.join("&", $params);
        }
        if ($this->medium)
            $url .= '.'.$this->medium;
        if ($this->anquor)
            $url .= '#'.$this->anquor;
        return $url;
    }

}
?>
